<template>
  <div>
    <!-- 面包屑导航 -->
    <el-header>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
      </el-breadcrumb>
    </el-header>

    <!-- 主体部分 -->
    <el-card>
      <!-- 搜索框 -->
      <el-input class="search" placeholder="请输入内容" @clear="getTableData"
                v-model="search" clearable @keyup.enter.native="getTableData">
        <template slot="append">
          <!-- 搜索按钮 -->
          <el-button icon="el-icon-search" @click="getTableData"></el-button>
        </template>
      </el-input>

      <!-- 添加商品按钮 -->
      <el-button type="primary" @click="$router.push('/goods/add')">添加商品</el-button>

      <!-- 商品列表table -->
      <el-table :data="tableData" border stripe>
        <el-table-column type="index" label="" width="50px"></el-table-column>
        <el-table-column prop="name" label="商品名称"></el-table-column>
        <el-table-column prop="price" label="商品价格" width="120px"></el-table-column>
        <el-table-column prop="category__name" label="商品分类" width="120px"></el-table-column>
        <el-table-column label="创建时间" width="200px">
          <template slot-scope="scope">
            {{ scope.row.date|standardTime }}
          </template>
        </el-table-column>
        <!-- 操作列 -->
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button type="primary" icon="el-icon-edit" size="mini">修改</el-button>
            <!-- 删除按钮 -->
            <el-button type='danger' icon="el-icon-delete"
                       size="mini" @click="delGood(scope.row.id)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 底部分页区域 -->
      <el-pagination
              @size-change="sizeChange"
              @current-change="currentChange"
              :current-page="page"
              :page-sizes="[5, 8, 12, 16]"
              :page-size="num"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'Goods',
  data() {
    return {
      // 搜索框绑定值
      search: '',
      // 商品表格数据
      tableData: [],
      // 当前页号
      page: 1,
      // 每页数量，默认为8
      num: 8,
      // 数据总条数
      total: 0
    }
  },
  filters: {
    // 时间格式化
    standardTime(time) {
      let res = new Date(time)
      // 不足补0
      let y = res.getFullYear().toString().padStart(4, '0')
      // 月份坑爹，1月为0
      let m = (res.getMonth() + 1).toString().padStart(2, '0')
      let d = res.getDate().toString().padStart(2, '0')
      let hh = res.getHours().toString().padStart(2, '0')
      let mm = res.getMinutes().toString().padStart(2, '0')
      let ss = res.getSeconds().toString().padStart(2, '0')
      return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
    }
  },
  methods: {
    // 获取所有商品列表
    async getTableData() {
      let { data: res } = await this.$ajax.get('goods/', {
        params: { page: this.page, num: this.num, search: this.search }
      })
      if (res.status === 'success') {
        this.tableData = res.data
        this.total = res.total
        this.$message.success('获取商品列表成功')
      } else
        this.$message.error('获取商品列表失败')
    },
    // 页面尺寸改变
    sizeChange(num) {
      this.num = num
      this.getTableData()
    },
    // 页码改变
    currentChange(page) {
      this.page = page
      this.getTableData()
    },
    // 删除商品点击调用
    delGood(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let { data: res } = await this.$ajax.post('goods/del/', this.$ajax.stringify({ id }))
        if (res.status === 'OK') {
          await this.getTableData()
          this.$message.success('删除商品成功')
        } else
          this.$message.error('删除商品失败')
      })
    }
  }
  ,
  created() {
    this.getTableData()
  }
}
</script>

<style scoped>
.el-header {
  display: flex;
  align-items: center;
}

.search {
  width: 300px;
  margin-right: 15px;
}

.el-table {
  margin-top: 15px;
}

.el-pagination {
  display: flex;
  justify-content: center;
  margin-top: 10px;
}
</style>